// pages/register/register.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		time: '120',
		on: '4',
		yzm1: '',
		iphone1: '',
		pd: '',
		uid: '',
		user: '',
		ssra:false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// action=setTjr&tjr=推荐人ID
		console.log(options)
		let that=this
		if(options.id!=undefined){
			this.setData({
				uid: options.id,
			})
		}
		
		wx.getStorage({
			key: 'user',
			success: (res) => {
				this.setData({
					user: res.data
				})
				console.log(this.data.user)
			}
		})
		wx.getStorage({
			key: 'tjrid',
			success: (e) => {
				wx.request({
					url: getApp().globalData.http + 'action=setTjr&tjr=' + e.data,
					success: (e) => {
						console.log(e)
						if(e.data.data.code!=undefined){
							this.setData({
								uid: e.data.data.code,
								ssra:true
							})
						}
					}
				})
			}
		})
	},
	yzm(e) {
		this.setData({
			yzm1: e.detail.value
		})
	},
	iphone(e) {
		this.setData({
			iphone1: e.detail.value
		})
	},
	uid(e) {
		this.setData({
			uid: e.detail.value
		})
	},
	click() {
		let that = this
		if (this.data.iphone1 != '') {
			wx.request({
				url: getApp().globalData.http + 'action=sendMsg&mobile=' + this.data.iphone1,
				success: function(res) {
					console.log(res)
					that.setData({
						pd: res.data.data.yzm
					})
				}
			})
			this.setData({
				on: 1
			})
			let haha = this.data.time
			let that = this
			let ontime = setInterval(function() {
				haha--
				if (that.data.time > 0) {
					that.setData({
						time: haha
					})
				} else {
					that.setData({
						time: 120
					})
					that.setData({
						on: 8
					})
					clearInterval(ontime)
				}
			}, 1000)
		} else {
			wx.showToast({
				icon: 'none',
				title: '手机号未输入'
			})
		}
	},
	btn() {
		let that = this
		// if (this.data.yzm1 == this.data.pd) {
		wx.request({
			url: getApp().globalData.http + 'action=wxBdMobile&unionid=' + this.data.user.unionId +
				'&nickname=' + this.data.user.nickName + '&sex=' + this.data.user.gender + '&headimgurl=' +
				this.data.user.avatarUrl + '&mobile=' + this.data.iphone1 + '&yzm=' + this.data.yzm1 + '&tjCode=' + this.data.uid,
			success: (res) => {
				wx.setStorage({
					key: 'uid',
					data: res.data.data
				})
				if (res.data.code == 1) {
					wx.switchTab({
						url: '../index/index?state=true'
					})
				} else {
					wx.showToast({
						icon: 'none',
						title: res.data.msg
					})
				}
				console.log(res)
			}
		})
		// }
	},
})


